package com.yochi.pictureseditor.workerthreads;

import com.yochi.pictureseditor.factories.BitmapFactoryAdapter;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;

public class BitmapLoadingWorkerTask extends AsyncTask<Void, Void, Bitmap> {
	
    private final Uri uri;
    private final int requiredSize;
    private final BitmapLoaderListener listener;

    public BitmapLoadingWorkerTask(Uri uri, int requiredSize, BitmapLoaderListener listener) {
    	this.uri = uri;
    	this.requiredSize = requiredSize;
    	this.listener = listener;
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(Void... params) {
        return BitmapFactoryAdapter.decodeUri(uri, requiredSize);
    }

    // Once complete, post callback
    @Override
    protected void onPostExecute(Bitmap bitmap) {
       if(listener != null) {
    	   listener.bitmapLoaded(bitmap);
       }
    }
    
    // Listener interface 
    public interface BitmapLoaderListener {
    	public void bitmapLoaded(Bitmap bitmap);
    }
    
}